package com.ddstation.crm.evaluate.controller;

import com.ddstation.crm.institution.entity.DtHealthManageInstitutionEntity;
import com.ddstation.crm.institution.mapper.DtHealthManageInstitutionMapper;
import com.ddstation.doctor.mapper.DdDoctorMapper;
import com.ddstation.exam.entity.DtHealthManageInstitutionGroupEntity;
import com.ddstation.exam.entity.DtInvsInstitutionEvaluateEntity;
import com.ddstation.exam.mapper.DtInvsInstitutionEvaluateMapper;
import com.ddstation.exchange.service.DtHealthManageInstitutionGroupService;
import com.ddstation.instrument_disinfect.model.InstrumentDisinfectDetailParModel;
import com.ddstation.instrument_disinfect.model.InstrumentDisinfectHeadResModel;
import com.ddstation.instrument_disinfect.service.InstrumentDisinfectService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * 评价
 */
@Controller
public class EvaluateController {


	@RequestMapping(value="evaluate.do")
	public String evaluate(Model model, HttpSession session,
		@RequestParam String institutionId) {
		//机构信息
		DtHealthManageInstitutionEntity institutionEntity = dtHealthManageInstitutionMapper.select(institutionId);
		if(institutionEntity != null){
			//医生信息
			List<Map<String,Object>> doctorList = ddDoctorMapper.selectDoctorByInstitutionIdAndRole(institutionId, 1);
			model.addAttribute("doctorList",doctorList);
			model.addAttribute("doctorCount",doctorList.size());
			//护士信息
			List<Map<String,Object>> nurseList = ddDoctorMapper.selectDoctorByInstitutionIdAndRole(institutionId, 2);
			model.addAttribute("nurseList",nurseList);
			model.addAttribute("nurseCount",nurseList.size());
			//卫生消毒
			DtHealthManageInstitutionGroupEntity entity = new DtHealthManageInstitutionGroupEntity();
			entity.setParentInstitutionId(institutionId);
			List<DtHealthManageInstitutionGroupEntity> dtHealthManageInstitutionGroupList = dtHealthManageInstitutionGroupService.select(entity);
			//设置机构ID集合，必定存在父机构ID
			List<String> institutionIdList = new ArrayList<>();
			institutionIdList.add(institutionId);
			if(dtHealthManageInstitutionGroupList != null && dtHealthManageInstitutionGroupList.size() > 0){
				//存在子ID则全部加入in条件查询
				for(DtHealthManageInstitutionGroupEntity dtHealthManageInstitutionGroup:dtHealthManageInstitutionGroupList){
					institutionIdList.add(dtHealthManageInstitutionGroup.getChildInstitutionId());
				}
			}
			InstrumentDisinfectDetailParModel _in = new InstrumentDisinfectDetailParModel();
			_in.setInstitutionIdList(institutionIdList);
			_in.setLimit(0);
			_in.setOffset(7);
			_in.setBusinessSubType(101);//器械消毒
			//数据查询
			List<InstrumentDisinfectHeadResModel> apparatusList = instrumentDisinfectService.searchDetailList(_in);
			model.addAttribute("apparatusList",apparatusList);
			model.addAttribute("apparatusCount",apparatusList.size());
			_in.setBusinessSubType(102);//场所消毒
			//数据查询
			List<InstrumentDisinfectHeadResModel> placeList = instrumentDisinfectService.searchDetailList(_in);
			model.addAttribute("placeList",placeList);
			model.addAttribute("placeCount",placeList.size());
		}else{
			institutionEntity = new DtHealthManageInstitutionEntity();
			institutionEntity.setId(institutionId);
			institutionEntity.setName("未查询到相关信息");
		}
		model.addAttribute("institutionEntity",institutionEntity);
		return "pc/evaluate/evaluate";
	}


	@RequestMapping(value="evaluate.do/save", method= RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> evaluateSave(
			@RequestBody DtInvsInstitutionEvaluateEntity entity,
			HttpSession session) {
		Map<String, Object> ret = new HashMap<>();
		ret.put("errorCode", -1);
		if(entity.getInstitutionId() == null){
			ret.put("errorMsg", "获取评价机构ID号失败");
			return ret;
		}
		if(entity.getSatisfaction() == null){
			ret.put("errorMsg", "必须选择满意度");
			return ret;
		}
		switch (entity.getSatisfaction()){
			case "2":
				if(entity.getPhoneNumber() == null || entity.getPhoneNumber().length() == 0){
					ret.put("errorMsg", "联系方式不能为空");
					return ret;
				}
			case "1":
			case "0":
				//提交评价
				dtInvsInstitutionEvaluateMapper.insertSelective(entity);
				ret.put("errorCode", 0);
				break;
			default:
				ret.put("errorMsg", "必须选择满意度");
		}
		return ret;
	}

	@Inject DtInvsInstitutionEvaluateMapper dtInvsInstitutionEvaluateMapper;
	@Inject DtHealthManageInstitutionMapper dtHealthManageInstitutionMapper;
	@Inject DdDoctorMapper ddDoctorMapper;
	@Inject DtHealthManageInstitutionGroupService dtHealthManageInstitutionGroupService;
	@Inject InstrumentDisinfectService instrumentDisinfectService;


}
